﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Area
{
    public class EditAreaModel
    {
        public int AreaId { get; set; }

        [DisplayName("Nombre del Area")]
        [Required(ErrorMessage = "El nombre del proceso es requerido")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras o números")]
        public string Name { get; set; }

        [DisplayName("Jefatura/Gerencia")]
        [Required(ErrorMessage = "Una jefatura/gerencia es necesaria")]
        public int Management { get; set; }

        [DisplayName("Proceso")]
        [Required(ErrorMessage = "El proceso es necesario")]
        public int Process { get; set; }

        public IEnumerable<SelectListItem> Managements
        {
            get
            {
                return managementList;
            }
        }

        public IEnumerable<SelectListItem> Processes
        {
            get
            {
                return processList;
            }
        }

        private List<SelectListItem> managementList;
        private List<SelectListItem> processList;

        riskanalysisEntities entities = new riskanalysisEntities();

        public EditAreaModel()
        {
            FillList();
        }

        public EditAreaModel(int areaId)
            : this()
        {
            FillList();
            FillAreaById(areaId);
        }

        private void FillList()
        {
            managementList = new List<SelectListItem>();

            var queryManagements = from m in entities.managements
                                   where m.enabled
                                   select m;

            foreach (management item in queryManagements)
            {
                managementList.Add(
                    new SelectListItem
                    {
                        Text = item.name,
                        Value = item.managementId.ToString()
                    });
            }
            processList = RegisterAreaModel.FillProcessList(queryManagements.First().managementId);
        }

        private void FillAreaById(int id)
        {
            area theArea = GetAreaById(id);

            AreaId = theArea.areaId;
            Name = theArea.name;
            Management = theArea.process.managementId;
            processList = RegisterAreaModel.FillProcessList(Management);
            Process = theArea.processId;
        }

        private area GetAreaById(int id)
        {
            var query = from a in entities.areas
                        where a.areaId == id && a.enabled
                        select a;

            area theArea = query.FirstOrDefault();

            if (theArea == null)
                throw new EditAreaException("El area que usted busca no existe");
            return theArea;
        }

        public void Save()
        {
            area theArea = GetAreaById(AreaId);

            theArea.name = Name;
            theArea.processId = Process;           
            entities.SaveChanges();
        }
    }
}